//
//  TT_AlertManager.m
//  BaseProject
//
//  Created by 王洋 on 2020/12/4.
//  Copyright © 2020 王洋. All rights reserved.
//

#import "TT_AlertManager.h"
#import "SCLAlertView.h"

@interface TT_AlertManager ()

@property (nonatomic, strong) SCLAlertView *alert;
@end

@implementation TT_AlertManager

+ (TT_AlertManager *)manager {
    
    static TT_AlertManager *shareInstance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken,^{
        shareInstance = [TT_AlertManager new];
    });
    return shareInstance;
}

- (void)alert:(NSString *)message {
    self.alert = [[SCLAlertView alloc] initWithNewWindowWidth:TTScreenWidth - 60];
    self.alert.showAnimationType = SCLAlertViewShowAnimationSlideInToCenter;
    [self.alert showTitle:@"温馨提示" subTitle:message style:SCLAlertViewStyleInfo closeButtonTitle:@"确定" duration:0.0];
}

- (void)alert:(NSString *)message doneAction:(void (^)(void))doneAction {
    self.alert = [[SCLAlertView alloc] initWithNewWindowWidth:TTScreenWidth - 60];
    self.alert.showAnimationType = SCLAlertViewShowAnimationSlideInToCenter;
    SCLButton *doneButton = [self.alert addButton:@"确定" actionBlock:^{
        doneAction();
    }];

    doneButton.buttonFormatBlock = ^NSDictionary *{
        NSMutableDictionary *buttonConfig = [[NSMutableDictionary alloc] init];
        buttonConfig[@"backgroundColor"] = [UIColor blueColor];
        buttonConfig[@"textColor"] = [UIColor whiteColor];

        return buttonConfig;
    };
    
    [self.alert showTitle:@"温馨提示" subTitle:message style:SCLAlertViewStyleInfo closeButtonTitle:nil duration:0.0];
}

- (void)alert:(NSString *)message cancleBtnTitle:(NSString *)cancleTitle cancleAction:(void (^)(void))cancleAction doneBtnTitle:(NSString *)doneTitle doneAction:(void (^)(void))doneAction {
    
    self.alert = [[SCLAlertView alloc] initWithNewWindowWidth:TTScreenWidth - 60];
    self.alert.showAnimationType = SCLAlertViewShowAnimationSlideInToCenter;
    
    [self.alert setHorizontalButtons:YES];
    
    SCLButton *cancleButton = [self.alert addButton:cancleTitle actionBlock:^{
        cancleAction();
    }];
    
    cancleButton.buttonFormatBlock = ^NSDictionary *{
        NSMutableDictionary *buttonConfig = [[NSMutableDictionary alloc] init];
        buttonConfig[@"backgroundColor"] = [UIColor whiteColor];
        buttonConfig[@"textColor"] = [UIColor blackColor];
        buttonConfig[@"borderWidth"] = @1.0f;
        buttonConfig[@"borderColor"] = [UIColor lightGrayColor];
        return buttonConfig;
    };
    
    SCLButton *doneButton = [self.alert addButton:doneTitle actionBlock:^{
        doneAction();
    }];

    doneButton.buttonFormatBlock = ^NSDictionary *{
        NSMutableDictionary *buttonConfig = [[NSMutableDictionary alloc] init];
        buttonConfig[@"backgroundColor"] = [UIColor redColor];
        buttonConfig[@"textColor"] = [UIColor whiteColor];
        return buttonConfig;
    };
    
    [self.alert showTitle:@"温馨提示" subTitle:message style:SCLAlertViewStyleNotice closeButtonTitle:nil duration:0.0];
}


- (void)alertTitle:(NSString *)title message:(NSString *)message leftBtnTitle:(NSString *)leftTitle leftAction:(void (^)(void))leftAction rightBtnTitle:(NSString *)rightTitle rightAction:(void (^)(void))rightAction cancleAction:(void (^)(void))cancleAction {
    
    if ([self.alert isVisible]) {
        [self.alert hideView];
    }
    
    self.alert = [[SCLAlertView alloc] initWithNewWindowWidth:TTScreenWidth - 60];
    self.alert.showAnimationType = SCLAlertViewShowAnimationSlideInToCenter;
    
    [self.alert removeTopCircle];//去掉头部圆圈
    self.alert.cornerRadius = 8.0f;
    
    [self.alert setHorizontalButtons:YES];
    
    UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [closeBtn setImage:[UIImage imageNamed:@"close_gray"] forState:UIControlStateNormal];
    closeBtn.frame = CGRectMake(TTScreenWidth - 100, 0, 40, 40);
    [closeBtn addGestureTapEventHandle:^(id  _Nonnull sender, UITapGestureRecognizer * _Nonnull gestureRecognizer) {
        if ([self.alert isVisible]) {
            [self.alert hideView];
        }
        cancleAction();
    }];
    
    [self.alert.view addSubview:closeBtn];
    
    SCLButton *leftButton = [self.alert addButton:leftTitle actionBlock:^{
        leftAction();
    }];
    
    leftButton.buttonFormatBlock = ^NSDictionary *{
        NSMutableDictionary *buttonConfig = [[NSMutableDictionary alloc] init];
        buttonConfig[@"backgroundColor"] = [UIColor whiteColor];
        buttonConfig[@"textColor"] = UIColorHex(0xff5539);
        buttonConfig[@"borderWidth"] = @1.0f;
        buttonConfig[@"borderColor"] = UIColorHex(0xff5539);
        return buttonConfig;
    };
    
    SCLButton *rightButton = [self.alert addButton:rightTitle actionBlock:^{
        rightAction();
    }];

    rightButton.buttonFormatBlock = ^NSDictionary *{
        NSMutableDictionary *buttonConfig = [[NSMutableDictionary alloc] init];
        buttonConfig[@"backgroundColor"] = UIColorHex(0xFF5539);
        buttonConfig[@"textColor"] = [UIColor whiteColor];
        return buttonConfig;
    };
    
    [self.alert showTitle:title subTitle:message style:SCLAlertViewStyleNotice closeButtonTitle:nil duration:0.0];
    self.alert.labelTitle.font = [UIFont systemFontOfSize:16 weight:UIFontWeightSemibold];
    self.alert.labelTitle.textColor = UIColorHex(0x222222);
    
    self.alert.viewText.font = [UIFont systemFontOfSize:14];
    self.alert.viewText.textColor = UIColorHex(0x666666);
}



- (SCLAlertView *)alert {
    if (!_alert) {
        _alert = [[SCLAlertView alloc] initWithNewWindowWidth:TTScreenWidth - 60];
        _alert.showAnimationType = SCLAlertViewShowAnimationSlideInToCenter;

    }
    return _alert;
}

@end
